package com.hxzy.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author nick
 */
@WebServlet("/dispatcher")
public class DispatcherServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //页面转发通常和request作用域一起搭配使用
        req.setAttribute("teacher", "苍老师");

        /*
            相当于是在项目的context path之后加上url映射。http://localhost:8080/jsp02_war_exploded/dispatcher2
        *   req.getRequestDispatcher("/dispatcher2");
        *
        *   相当于对首页进行跳转http://localhost:8080/jsp02_war_exploded/index.jsp
        *   req.getRequestDispatcher("/index.jsp");
        *
        *   getRequestDispatcher("/") 相当于是项目中webapp目录
        * */

        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/dispatcher2");
        //页面转发
        requestDispatcher.forward(req,resp);
    }
}
